import { Module, ActionContext } from 'vuex'
import { axiosGet } from "@/api/ajaxCommon"
import { getCookie, setCookie, delCookie } from '@/utils/cookie'

interface SessionState {
    application: any,
    user: any,
    tenant: any
}
class SessionStore implements Module<SessionState, any>{
    namespaced = true;
    state = {
        application: null,
        user: null,
        tenant: null
    }
    mutations = {
        SET_TOKEN(state, token) {
            setCookie('Token', token)
        },
        SET_USER(state, Info) {
            if (Info) {
                // state.user = Info;
                setCookie('User', Info)
            } else {
                // state.user = {}
            }
        },
        OUT_LOGIN(state) {
            state.user = {}
            delCookie('Token')
            delCookie('User')
        }

    };
    actions = {
        async init(content: ActionContext<SessionState, any>) {
            axiosGet('Session/GetCurrentLoginInformations', '').then(data => {
                content.state.application = data.application;
                content.state.user = data.user;
                content.state.tenant = data.tenant;
            })
        },
        async setToken(content: { commit }, token: any) {
            content.commit('SET_TOKEN', token)
        },
        setUser: async ({ commit }, user) => {
            commit('SET_USER', user)
        },
        outLogin({ commit }) {
            commit('OUT_LOGIN')
        }
    }
}
const session = new SessionStore();
export default session;